package com.platform.modules.order;

import com.platform.common.exception.BusinessException;
import com.platform.common.utils.*;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallOrderEntity;
import com.platform.modules.mall.entity.MallUserCardEntity;
import com.platform.modules.mall.entity.MallWhitelistEntity;
import com.platform.modules.mall.service.MallGoodsService;
import com.platform.modules.mall.service.MallGoodsToActivityService;
import com.platform.modules.mall.service.MallOrderGoodsService;
import com.platform.modules.mall.service.MallWhitelistService;
import com.platform.modules.sys.service.SysConfigService;

/**
 * 白名单版活动订单
 */
public class WActivityOrderBuild extends ActivityOrderBuild {

    private MallWhitelistService whitelistService;
    private MallOrderGoodsService orderGoodsService;

    public WActivityOrderBuild(SysConfigService sysConfigService,
                               JedisUtil jedisUtil,
                               MallGoodsToActivityService goodsToActivityService,
                               MallWhitelistService whitelistService,
                               MallOrderGoodsService orderGoodsService,
                               MallGoodsService goodsService) {
        super(sysConfigService, jedisUtil, goodsToActivityService,goodsService,orderGoodsService);
        this.whitelistService = whitelistService;
        this.orderGoodsService = orderGoodsService;
    }

    @Override
    public void paramsVerify() {
        super.paramsVerify();
        if (StringUtils.isBlank(user.getMobile())) {
            throw new BusinessException("请先绑定手机号码！");
        }
        String phone = Md5Utils.encryption32(user.getMobile());
        switch (params.getActivityId()) {
            case Constant.TTH_CARD:
                whitelistService.isValidByPhone(phone, MallWhitelistEntity.Type.TTH_CARD.getValue());
                break;
            case Constant.TTH_COUPON:
                if(!whitelistService.reduceByPhone(phone, 1, MallWhitelistEntity.Type.TTH_COUPON.getValue())){
                    throw new BusinessException("可用购券次数不足");
                }
                break;
            default:
                throw new BusinessException("未匹配到指定的活动！");
        }
    }

    @Override
    public void limited() {
        if(entity.getIsLimited() == null || entity.getIsLimited() < 1){
            return;
        }
        MallUserCardEntity userCardEntity = new MallUserCardEntity();
        userCardEntity.setUserId(user.getId());
        // 本次活动的开始时间
        userCardEntity.setAddTime(DateUtils.strToDate(activityStatus.getDate()[0].toString()));
        // 本次活动的ID
        userCardEntity.setActivityId(params.getActivityId());
        // 用户手机号码
        userCardEntity.setUserMobile(user.getMobile());
        MallOrderEntity orderGoodsEntity = orderGoodsService.queryUserRecord(userCardEntity);
        if (orderGoodsEntity != null && orderGoodsEntity.getNumber() != null && orderGoodsEntity.getNumber() + params.getNumber() > entity.getIsLimited()) {
            if (orderGoodsEntity.getOrderStatus().equals(Constant.OrderStatus.DFK.getValue())) {
                throw new BusinessException("当前存在一次 " + activityStatus.getExtend1() + " 订单未支付，是否需要完成待支付订单?", 901);
            }
            throw new BusinessException("非常抱歉，" + activityStatus.getExtend1() + "领取名额您已用完");
        }
//        switch (params.getActivityId()) {
//            case Constant.TTH_CARD:
//                super.limited();
//                break;
//            case Constant.TTH_COUPON:
//                MallUserCardEntity userCardEntity = new MallUserCardEntity();
//                userCardEntity.setUserId(user.getId());
//                // 本次活动的开始时间
//                userCardEntity.setAddTime(DateUtils.strToDate(activityStatus.getDate()[0].toString()));
//                // 本次活动的ID
//                userCardEntity.setActivityId(params.getActivityId());
//                // 用户手机号码
//                userCardEntity.setUserMobile(user.getMobile());
//                MallOrderEntity orderGoodsEntity = orderGoodsService.queryUserRecord(userCardEntity);
//                if (orderGoodsEntity != null && orderGoodsEntity.getNumber() != null && orderGoodsEntity.getNumber() + params.getNumber() > entity.getIsLimited()) {
//                    if (orderGoodsEntity.getOrderStatus().equals(Constant.OrderStatus.DFK.getValue())) {
//                        throw new BusinessException("当前存在一次 《" + activityStatus.getExtend1() + "》 订单未支付，是否需要完成待支付订单?", 901);
//                    }
//                    throw new BusinessException("非常抱歉，" + activityStatus.getExtend1() + "领取名额您已用完");
//                }
//                break;
//            default:
//                throw new BusinessException("未匹配到指定的活动！");
//        }
    }

    @Override
    public MallOrderEntity orderInfo(MallGoodsEntity goods) {
        MallOrderEntity orderInfo = super.orderInfo(goods);
//

        return orderInfo;
    }

}
